package com.longrise.customview.photo;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.longrise.android.util.Util;
import com.longrise.android.widget.LRelativeLayoutView;
import com.longrise.customview.R;
import com.longrise.customview.photo.utils.ImageLoader;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by chengsan on 17/5/2.
 */

public class PhotoAdapter extends BaseAdapter
{
    private Context context;
    private List<String> images;
    private Handler handler;
    private int mode = 2 ;//照片选择模式 2:多张 1:单张
    private int maxSelectPhotoNum = 5;//一次最多可以选择多少张图片
    /**
     * 用户选择的图片，存储为图片的完整路径
     */
    public List<String> mSelectedImage;

    /**
     * 文件夹路径
     */
    private String mDirPath;


    public PhotoAdapter(Context context,int mode,int maxSelectPhotoNum) {
        this.context = context;
        mSelectedImage = new LinkedList<String>();
        this.mode = mode;
        this.maxSelectPhotoNum = maxSelectPhotoNum;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void setmDirPath(String mDirPath) {
        this.mDirPath = mDirPath;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    public List<String> getmSelectedImage()
    {
        return mSelectedImage;
    }

    @Override
    public int getCount()
    {
        if(null != images)
        {
            return images.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i)
    {
        if(null != images && images.size() > i)
        {
            return images.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i)
    {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup)
    {
        ViewHolder viewHolder;
        if(view == null)
        {
            viewHolder = new ViewHolder();
            PhotoItem photoItem = new PhotoItem(context);
            viewHolder.imageView = photoItem.imageView;
            viewHolder.selectIcon = photoItem.selectIcon;

            view = photoItem;

            view.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) view.getTag();
        }

        final String string  = images.get(i);

        // 已经选择过该图片
        if (mSelectedImage.contains(mDirPath + "/" + string))
        {
            viewHolder.selectIcon.setBackgroundResource(R.drawable.im_pictures_selected);
            viewHolder.imageView.setColorFilter(Color.parseColor("#77000000"));
        }
        else
        {
            viewHolder.selectIcon.setBackgroundResource(R.drawable.im_picture_unselected);
            viewHolder.imageView.setColorFilter(null);
        }

        setImageByUrl(mDirPath + "/" + string,viewHolder.imageView);
        viewHolder.imageView.setOnClickListener(new View.OnClickListener()
        {
            //选择，则将图片变暗，反之则反之
            @Override
            public void onClick(View view)
            {
                // 已经选择过该图片
                if (mSelectedImage.contains(mDirPath + "/" + string))
                {
                    mSelectedImage.remove(mDirPath + "/" + string);
                }
                else
                {
                    if(mode == 1)
                    {
                        if(mSelectedImage != null && mSelectedImage.size() >= 1)
                        {
                            mSelectedImage.remove(0);
                        }
                    }

                    if(mSelectedImage != null && mSelectedImage.size() >= maxSelectPhotoNum)
                    {
                        Toast.makeText(context,"一次最多只能选择"+maxSelectPhotoNum+"张",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    mSelectedImage.add(mDirPath + "/" + string);
                    // 未选择该图片
                }

                if(null != handler)
                {
                    Message message = new Message();
                    message.what = 200;
                    message.arg1 = mSelectedImage.size();
                    handler.sendMessage(message);
                }
                notifyDataSetChanged();


            }
        });

        return view;
    }

    /**
     * 为ImageView设置图片
     *
     * @return
     */
    public void setImageByUrl(String url,ImageView imageView)
    {
        ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(url, imageView);
    }

    class ViewHolder
    {
        public ImageView imageView;
        public ImageButton selectIcon;
    }

    class PhotoItem extends LRelativeLayoutView
    {
        public ImageView imageView;
        public ImageButton selectIcon;

        public PhotoItem(Context context)
        {
            super(context);
            init(context);
        }

        private void init(Context context)
        {
            int bgColor = Color.parseColor("#00000000");
            this.setBackgroundColor(bgColor,bgColor,bgColor,bgColor,bgColor,0, Util.dip2px(context,1),Color.LTGRAY);

            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            this.addView(imageView,LayoutParams.MATCH_PARENT, Util.dip2px(context,100));

            selectIcon = new ImageButton(context);
            selectIcon.setClickable(false);
            LayoutParams selectLP = new LayoutParams(Util.dip2px(context,20),Util.dip2px(context,20));
            selectLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            selectLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            selectLP.rightMargin = Util.dip2px(context,3);
            selectLP.topMargin = Util.dip2px(context,3);
            this.addView(selectIcon,selectLP);

        }
    }
}
